# _*_ coding:utf-8 _*_

"""
# @Time　 : 2021/12/29 22:14
# @Author : king
# @File　 : case_handle.py
# @project: ApiPytestFramework
# @QQ     : 772084279
# @Email  : 772084279@qq.com
# @blog   : https://ceshizhilu.blog.csdn.net/
"""
import os

from common_utils.api_path import config_path, data_path
from common_utils.excel_handle import ReadExcel
from common_utils.gen_case import gen_case
from common_utils.global_vars import CaseType
from common_utils.yaml_handle import ReadYaml


def get_case_data():
    case_type = ReadYaml(config_path + "config.yaml").read_yaml["case"]
    if case_type == CaseType.EXCEL.value:
        cases = []
        for file in [excel for excel in os.listdir(data_path) if os.path.splitext(excel)[1] == ".xlsx"]:
            data = ReadExcel(data_path + file).read_excel()
            name = os.path.splitext(file)[0]
            class_name = name.split("_")[0].title() + name.split("_")[1].title()
            gen_case(name, data, class_name)
            cases.extend(data)
        return cases
    elif case_type == CaseType.YAML.value:
        cases = []
        for yaml_file in [yaml for yaml in os.listdir(data_path) if
                          os.path.splitext(yaml)[1] in [".yaml", "yml"]]:
            data = ReadYaml(data_path + yaml_file).read_yaml
            name = os.path.splitext(yaml_file)[0]
            class_name = name.split("_")[0].title() + name.split("_")[1].title()
            gen_case(name, data, class_name)
            cases.extend(data)
        return cases
    else:
        cases = []
        for file in [excel for excel in os.listdir(data_path) if
                     os.path.splitext(excel)[1] in [".yaml", "yml", ".xlsx"]]:
            if os.path.splitext(file)[1] == ".xlsx":
                data = ReadExcel(data_path + file).read_excel()
                name = os.path.splitext(file)[0]
                cases.extend(data)
            else:
                data = ReadYaml(data_path + file).read_yaml
                name = os.path.splitext(file)[0]
                cases.extend(data)

            class_name = name.split("_")[0].title() + name.split("_")[1].title()
            gen_case(name, data, class_name)
        return cases
